package com.sh.wy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sh.util.SystemConstant;
import com.sh.wy.system.entity.SysMenu;
import com.sh.wy.system.mapper.LiveUserMapper;
import com.sh.wy.system.mapper.SysMenuMapper;
import com.sh.wy.system.mapper.SysUserMapper;
import com.sh.wy.system.entity.LiveUser;
import com.sh.wy.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Auther: 世豪学java
 * @Date: 2023/12/17 - 12 - 17 - 13:02
 * @Decsription: com.sh.wy.web.system.service.impl
 * @version: 1.0
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {
    @Autowired
    private SysMenuMapper menuMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private LiveUserMapper liveUserMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //接收到到username是带有用户类型的，所以需要拆分
        // admin:1 1代表物主身份，0代表业主身份获取用户身份
        //username.substring(username.indexOf(":")+1) 用户类型
        Integer userType = Integer.parseInt( username.substring(username.indexOf(":")+1));
        //获取用户名
        username = username.substring(0,username.indexOf(":"));
        //身份查询，从数据库做查询
        //物主
        if(userType== SystemConstant.USER_TYPE_WUZHU){
            QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(SysUser::getUsername,username);
            SysUser user = sysUserMapper.selectOne(wrapper);
            if (Objects.isNull(user)){
                throw new RuntimeException("用户名错误");
            }
            Integer isAdmin = user.getIsAdmin();
            List<String> menus;
            if (isAdmin == 1){
                //超级管理员，查询所有权限
                List<SysMenu> menuList = menuMapper.selectList(null);
                menus = menuList.stream()
                        .map(item -> item.getMenuCode())
                        .collect(Collectors.toList());
            }else {
                menus = sysUserMapper.getMenus(user.getUserId());
            }
            user.setMenus(menus.toArray(new String[menus.size()]));
            return user;
        }//业主
        else if(userType== SystemConstant.USER_TYPE_YEZHU){
            QueryWrapper<LiveUser> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(LiveUser::getUsername,username);
            LiveUser user = liveUserMapper.selectOne(wrapper);
            if (Objects.isNull(user)){
                throw new RuntimeException("用户名错误");
            }
            //获取权限信信息
            List<String> menus = liveUserMapper.getMenus(user.getUserId());
            user.setMenus(menus.toArray(new String[menus.size()]));
            return user;
        }else {
            return null;
        }
    }
}
